const device = wx.getSystemInfoSync();
const app = getApp();
Page({
  data: {
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '文件上传', //导航栏 中间的标题
    },
    ratio: 240 / 151,
    pw: 240,
    ph: 152,
    type: 0,
    canvasW: 1487,
    canvasH: 2105
    //户口簿数量  540  396
  },
  onLoad: function (options) {
    let data = { type: options.type };
    if (options.type == 3) {
      data.ratio = 143 / 105;
      data.pw = 143 * 2;
      data.ph = 105 * 2;
    } else if (options.type == 4) {
      data.ratio = 297 / 420;
      data.pw = 297;
      data.ph = 420;
    } else if (options.type == 6) {

      this.initImage(options.photo);
    }
    this.setData(data);
  },
  initImage: function (url) {
    const that = this;
    let data = {};
    wx.getImageInfo({
      src: url,
      complete: (res) => {
        console.log(res);
        data.pw = 495;
        data.ph = res.height / (res.width / data.pw);
        data.ratio = data.pw / data.ph;
        data.originUrl = url;
        data.chooseType = 3;
        that.setData(data);
      }
    })
  },
  chooseWxImage: function (event, success) {
    let that = this;
    wx.showActionSheet({
      itemList: ['微信图片', '本地图片'],
      success: function (res) {
        const index = res.tapIndex;
        if (index === 1) {
          that.chooseUploadImage((url) => {
            if (success) success.call(this, url);
            else that.initImage(url);
          });
        } else {
          that.chooseMessageFile((url) => {
            if (success) success.call(this, url);
            else that.initImage(url);
          });
        }
      }
    })
  },
  chooseMessageFile: function (success) {
    wx.chooseMessageFile({
      count: 1,
      type: 'image',
      success: function (res) {
        const url = res.tempFiles[0].path;
        success.call(this, url);
      }
    })
  },
  chooseUploadImage: function (success) {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      complete: (res) => {
        const tempFilePaths = res.tempFilePaths;
        success.call(this, tempFilePaths[0]);
      },
    })
  },
  uploadCardPositive: function () {
    const that = this;
    //身份证正面照
    this.chooseUploadImage((url) => {
      // cardPositive
      that.setData({ originUrl: url, chooseType: 1 });
    })
  },
  toCropperPage(original) {
    wx.navigateTo({
      url: '/printer/cropper/cropper?url=' + original,
    })
  },
  getCropperImg: function (e) {
    let imageUrl = e.detail.url;
    console.log(e);
    let data = { originUrl: '' };
    if (this.data.chooseType === 1) data.cardPositive = imageUrl;
    else if (this.data.chooseType === 2) data.cardReverse = imageUrl;
    else data.license = imageUrl;
    this.setData(data);
  },
  uploadCardReverse: function () {
    //身份证反面照
    this.chooseUploadImage((url) => {
      // cardReverse
      this.setData({ originUrl: url, chooseType: 2 });
    })
  },
  uploadLicense: function () {
    //添加营业执照
    this.chooseUploadImage((url) => {
      // license
      this.setData({ originUrl: url, chooseType: 3 });
    })
  },
  getMergeImage: function (success) {
    const type = this.data.type;
    const that = this;
    //设置A4纸尺寸
    const width = that.data.canvasW;
    const heigth = that.data.canvasH;

    //进行图片合并排版
    const ctx = wx.createCanvasContext('share', this);
    // 绘制背景图
    ctx.drawImage('../images/bgA4.png', 0, 0, width, heigth);
    if (type == 2 || type == 5 || type == 3) {
      let pw = 0, ph = 0;
      if (type == 2 || type == 5) {
        pw = width * (85.6 * 1.25 / 210);
        ph = heigth * (54 * 1.25 / 297);
        // pw = width * 0.5;  //85.6 * 54.0  210 * 297
      } else {
        pw = width * 0.6;
        ph = that.data.ph * (pw / that.data.pw);
      }
      const left = (width - pw) / 2;
      const top = (heigth - ph * 2) / 2;

      if (that.data.cardPositive) {
        ctx.drawImage(that.data.cardPositive, left, top - 20, pw, ph);
        ctx.drawImage(that.data.cardReverse, left, top + ph + 20, pw, ph);
      } else {
        ctx.drawImage(that.data.cardReverse, left, top - 20, pw, ph);
      }
    } else if (type == 4) {
      const pw = width * 0.8;
      const ph = that.data.ph * (pw / that.data.pw);
      const left = (width - pw) / 2;
      const top = (heigth - ph) / 2;
      console.log(pw, ph);
      ctx.drawImage(that.data.license, left, top - 20, pw, ph);
    } else if (type == 6) {
      const pw = width * 0.8;
      const ph = that.data.ph * (pw / that.data.pw);
      const left = (width - pw) / 2;
      const top = (heigth - ph) / 2;
      console.log(pw, ph);
      ctx.drawImage(that.data.license, left, top - 20, pw, ph);
    }
    ctx.draw(true, () => {
      wx.canvasToTempFilePath({
        canvasId: 'share',
        x: 0,
        y: 0,
        width: width,
        height: heigth,
        destWidth: width,
        destHeight: heigth,
        success: function (res) {
          success.call(this, res.tempFilePath);
        },
        fail: function (res) {
          wx.showToast({
            title: '图片生成失败',
          })
        }
      }, that)
    })
  },
  nextPrinterStep: function () {
    let that = this;
    this.getMergeImage((url) => {
      console.log('图片开始生成', url)
      //TODO 跳转到订单页面
      // that.setData({ originUrl: url })
      wx.navigateTo({
        url: '/printer/order/order?url=' + url + "&prType=" + this.data.type
      })
      //上传图片
      // util.uploadImage(url, getApp().globalData.token, (data) => {
      //   console.log('文件上传成功', data);
      // })
    })
  }
})